package com.yanqu.road.server.cmd.minewar.task;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.minewar.UserMinePit;
import com.yanqu.road.pb.minewar.MineWarProto;
import com.yanqu.road.server.cmd.base.BaseTask;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.MineWarModule;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.player.MineWarMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

public class GetRewardTask extends BaseTask {

    private long getRewardTime;

    private Property occupyReward;

    public GetRewardTask(YanQuMessage packet, GamePlayer player) {
        super(packet, player);
    }

    @Override
    protected boolean parseData() throws Exception {
        return true;
    }

    @Override
    protected void execute() {
        getRewardTime = System.currentTimeMillis() / 1000;
        occupyReward = new Property();
        List<UserMinePit> userPitList = MineWarMgr.getUserMinePitList(player.getUserId());
        for(UserMinePit userMinePit : userPitList){
            long lastGetRewardTime = userMinePit.getLastGetRewardTime();
            Property reward = MineWarMgr.getPitOccupyReward(player.getUserId(), userMinePit, getRewardTime, true);
            occupyReward.addProperty(reward);
            LogMgr.addLogMineWarReward(player.getUserId(), userMinePit.getMineType(), userMinePit.getPitId(), PropertyHelper.parsePropertyToString(occupyReward),
                    getRewardTime - lastGetRewardTime, false, eLogMoneyType.MineWarGetOccupyReward.getValue());
        }
        //算广场的收益
        Property reward = player.getModule(MineWarModule.class).getOccupyReward(getRewardTime, 0);
        occupyReward.addProperty(reward);
        //收益入袋
        player.getModule(CurrencyModule.class).addCurrency(occupyReward, eLogMoneyType.MineWar, eLogMoneyType.MineWarGetOccupyReward);
        if(!StringUtils.isNullOrEmpty(PropertyHelper.parsePropertyToString(occupyReward))){//有领到收益
            //增加一次领取收益
            player.notifyListener(eGamePlayerEventType.MineWarGetRewardTimes.getValue(), 1);
        }
    }

    @Override
    protected void sendData(GamePlayer player) {
        MineWarProto.MineGetRewardRespMsg.Builder respMsg = MineWarProto.MineGetRewardRespMsg.newBuilder();
        respMsg.setLastGetTime(getRewardTime);
        respMsg.setOutputCount1(occupyReward.getCountByGoodsId(ConfigMgr.getMineOutputItemMap().get(1)).longValue());
        respMsg.setOutputCount2(occupyReward.getCountByGoodsId(ConfigMgr.getMineOutputItemMap().get(2)).longValue());
        respMsg.setOutputCount3(occupyReward.getCountByGoodsId(ConfigMgr.getMineOutputItemMap().get(3)).longValue());
        respMsg.setRet(0);
        player.sendPacket(Protocol.U_MINE_WAR_GET_REWARD, respMsg);
    }
}
